package com.example.demo;

import com.example.demo.DAO.CompanyDAO;
import com.example.demo.Entity.Company;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.List;
@WebServlet("/CompanyServlet")
@MultipartConfig
public class CompanyServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求字符编码
        request.setCharacterEncoding("UTF-8");
        // 获取请求操作类型
        String type = request.getParameter("type");
        CompanyDAO dao = new CompanyDAO();
        if("list".equals(type)){ //企业列表查询
            List<Company> list = dao.selectAll();
            // 在转发前必须执行
            request.setAttribute("list", list);
            // 请求转发到企业列表页面
            request.getRequestDispatcher("/manage/companyList.jsp").forward(request, response);
        }else if("updateSelect".equals(type)){ //企业信息查询
            // 从请求域取得companyid
            String companyIdStr = request.getParameter("companyId");
            int companyId = Integer.parseInt(companyIdStr);
            // 根据companyid 从数据库中查询对应企业的信息
            Company company = dao.selectById(companyId);
            // 将企业信息保存到请求域
            request.setAttribute("company", company);
            // 界面跳转。跳转到【manage/companyUpdate.jsp】
            request.getRequestDispatcher("/manage/companyUpdate.jsp").forward(request, response);
        } else if ("update".equals(type)) {
            // 获取企业更新信息
            int companyId = Integer.parseInt(request.getParameter("companyId"));
            String companyName = request.getParameter("companyName");
            String companyArea = request.getParameter("companyArea");
            String companySize = request.getParameter("companySize");
            String companyType = request.getParameter("companyType");
            String companyBrief = request.getParameter("companyBrief");
            int companyState = (request.getParameter("companyState") == null) ?
                    1 : Integer.parseInt(request.getParameter("companyState"));
            int companySort = (request.getParameter("companySort") == null) ?
                    0 : Integer.parseInt(request.getParameter("companySort"));
            int companyViewnum = (request.getParameter("companyViewnum") == null)
                    ? 0 : Integer.parseInt(request.getParameter("companyViewnum"));
            String companyOldPic = request.getParameter("companyOldPic");
            // 判断用户有无更新图片
            // 保存图片
            // 获取上传文件域
            Part part = request.getPart("companyPic");
            // 获取上传文件名称
            String fileName = part.getSubmittedFileName();
            if(!"".equals(fileName)) { // 用户有上传图片
                // 为防止上传文件重名，对文件进行重命名
               fileName = System.currentTimeMillis()
                        + fileName.substring(fileName.lastIndexOf("."));
                // 将上传的文件保存在服务器根目录下的upload/companyPic子目录下
                String filepath = getServletContext().getRealPath("/");
                String[] split = filepath.split("\\\\");
                String newPath = split[0] + "\\" + split[1] + "\\" + split[2] + "\\";
                newPath= newPath + "upload/companyPic";
                getServletContext().log("上传路径为：" +newPath);
                File f = new File(newPath);
                if (!f.exists()) {
                    f.mkdirs();
                }
                part.write(newPath + "/" + fileName);
            }else {
                 fileName = companyOldPic;
            }


            Company company = new Company(companyId,companyName,companyArea,
                    companySize,companyType,companyBrief,
                    companyState,companySort,companyViewnum,fileName);
            dao.update(company);
            response.sendRedirect("CompanyServlet?type=list");
        }


        }

}




